using System;
using System.Collections;
using System.Data;

using CE.Database;

/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.Provider
{
    public class ProviderPolicyDocumentDB
    {
        public ProviderPolicyDocumentDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public ProviderPolicyDocumentList GetProviderPolicyDocumentList(Int64 ProviderCompanyId, string ForWhat)
        {
            ProviderPolicyDocumentList _providerpolicydocumentlist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderPolicyDocSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@ForWhat", ForWhat);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _providerpolicydocumentlist = new ProviderPolicyDocumentList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        ProviderPolicyDocument _policydocument = new ProviderPolicyDocument();
                        _policydocument = FillDataRecord(_dt, i);
                        _providerpolicydocumentlist.Add(_policydocument);
                    }
                }
            }
            return _providerpolicydocumentlist;

        }

        public ProviderPolicyDocument GetProviderPolicyDocument(Int64 DocumentId)
        {
            ProviderPolicyDocument _policy = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderPolicyDocSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@DocumentId", DocumentId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _policy = new ProviderPolicyDocument();
                    _policy = FillDataRecord(_dt, 0);
                }
            }
            return _policy;
        }

        public Company CheckDocument(string DocName, string UploadPath)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocProviderPolicyDocCheckItem";
            Company _result = null;
            SortedList _sPara = new SortedList();

            _sPara.Add("@DocumentName", DocName);
            _sPara.Add("@UploadPath", UploadPath);

            //_result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public Int64 Insert(ProviderPolicyDocument doc)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderPolicyDocInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@DocumentId", doc.DocumentId);
            _sPara.Add("@DocumentName", doc.DocumentName);
            _sPara.Add("@Description", doc.Description);
            _sPara.Add("@UploadPath", doc.UploadPath);
            _sPara.Add("@SizeKB", doc.SizeKB);
            _sPara.Add("@ForWhat", doc.ForWhat);
            _sPara.Add("@ProviderCompanyId", doc.ProviderCompanyId);
            _sPara.Add("@RefCEUserId",doc.RefCEUserId);
            _sPara.Add("@Status", doc.Status);
            _sPara.Add("@Info1", doc.Info1);
            _sPara.Add("@Info2", doc.Info2);
            _sPara.Add("@Info3", doc.Info3);
            _sPara.Add("@Info4", doc.Info4);
            _sPara.Add("@Info5", doc.Info5);
            _sPara.Add("@Info6", doc.Info6);
            _sPara.Add("@Info7", doc.Info7);
            _sPara.Add("@Info8", doc.Info8);
            _sPara.Add("@Info9", doc.Info9);
            _sPara.Add("@Info10", doc.Info10);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete(Int64 DocumentId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderPolicyDocDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@DocumentId", DocumentId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;

        }


        private ProviderPolicyDocument FillDataRecord(DataTable myDataTable, int RowId)
        {
            ProviderPolicyDocument myPolicyDoc = new ProviderPolicyDocument();
            myPolicyDoc.DocumentId = Convert.ToInt64(myDataTable.Rows[RowId]["DocumentId"].ToString());
            myPolicyDoc.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myPolicyDoc.DocumentName = myDataTable.Rows[RowId]["DocumentName"].ToString();
            myPolicyDoc.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myPolicyDoc.UploadPath = myDataTable.Rows[RowId]["UploadPath"].ToString();
            myPolicyDoc.SizeKB = Convert.ToInt32(myDataTable.Rows[RowId]["SizeKB"].ToString());
            myPolicyDoc.ForWhat = myDataTable.Rows[RowId]["ForWhat"].ToString();
            myPolicyDoc.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myPolicyDoc.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            return myPolicyDoc;
        }

    }
}